﻿using AutoMapper;
using DDD.Application;
using GoodsModule.Domain.GoodsAggregate;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Logging;
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace GoodsModule.Application.Commands
{
    [AutoMapperTo(typeof(Domain.GoodsAggregate.GoodsImage))]
    public class SetGoodsImagesCommandItem
    {
        /// <summary>
        /// Id
        /// </summary>
        [Required]
        public int Id { get; set; } = 0;

        /// <summary>
        /// 排序
        /// </summary>
        [Comment("排序")]
        [Required]
        public uint Sort { get; set; } = 1000;

        /// <summary>
        /// 文件名
        /// </summary>
        [Comment("文件名")]
        [Required]
        [StringLength(100)]
        [MaxLength(100)]
        public string Name { get; set; } = string.Empty;

        /// <summary>
        /// 文件路径
        /// </summary>
        [Comment("文件路径")]
        [Required]
        [StringLength(100)]
        [MaxLength(100)]
        public string Path { get; set; } = string.Empty;

        /// <summary>
        /// 扩展名
        /// </summary>
        [Comment("扩展名")]
        [Required]
        [StringLength(10)]
        [MaxLength(10)]
        public string Ext { get; set; } = string.Empty;

        /// <summary>
        /// 文件大小
        /// </summary>
        [Comment("文件大小")]
        [Required]
        [Range(0, uint.MaxValue)]
        public uint Size { get; set; }

        /// <summary>
        /// 标题
        /// </summary>
        [Comment("标题")]
        [Required]
        [StringLength(100)]
        [MaxLength(100)]
        public string Title { get; set; } = string.Empty;

        /// <summary>
        /// 标签
        /// </summary>
        [Comment("标签")]
        [Required]
        [StringLength(1000)]
        [MaxLength(1000)]
        public string Tags { get; set; } = ",";
    }

    public class SetGoodsImagesCommand : ICommand<List<Domain.GoodsAggregate.GoodsImage>>
    {
        public int GoodsId { get; set; }

        public List<SetGoodsImagesCommandItem> goodsImages { get; set; } = new List<SetGoodsImagesCommandItem>();
    }

    public class SetGoodsImagesCommandHandler(Infrastructure.Repositories.GoodsRepository repository, IMapper mapper, ILogger<SetGoodsImagesCommandItem> logger)
        : ICommandHandler<SetGoodsImagesCommand, List<Domain.GoodsAggregate.GoodsImage>>
    {
        public async Task<List<Domain.GoodsAggregate.GoodsImage>> Handle(SetGoodsImagesCommand request, CancellationToken cancellationToken)
        {
            //TODO: 思考 需不需要领域服务，直接把领域服务的功能放这？
            var goods = await repository.GetAll().Include(e => e.ImageList).FirstAsync(e => e.Id == request.GoodsId);

            if (goods == null)
            {
                throw new InvalidOperationException($"找不到id为{request.GoodsId}的商品");
            }
            foreach (var image in request.goodsImages)
            {
                goods.SetImages(mapper.Map<Domain.GoodsAggregate.GoodsImage>(image));
            }
            await repository.UnitOfWork.SaveUnitOfWorkAsync();
            logger.LogInformation("SetGoodsImagesCommandHandler Goods {}", goods.Id);
            return goods.ImageList;
        }
    }
}
